package com.dwxt.cdms.service;

import com.dwxt.cdms.admin.service.HistoryServiceAdmin;
import com.dwxt.cdms.api.common.utils.DateUtil;
import com.dwxt.cdms.base.OperateCategory;
import com.dwxt.cdms.base.Page;
import com.dwxt.cdms.dao.BlutdruckMapper;
import com.dwxt.cdms.entity.Blutdruck;
import com.dwxt.cdms.entity.oufu.OuFuData;
import com.dwxt.cdms.entity.vo.BlutdruckCustom;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.dwxt.common.util.DateUtils;
import com.dwxt.common.util.IDUtils;
import com.dwxt.common.util.JsonUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BlutdruckService {

    @Autowired
    private BlutdruckMapper blutdruckMapper;

    @Autowired
    private HealthService healthService;

    @Autowired
    private EquipmentCustomService equipmentCustomService;

    @Autowired
    private HistoryServiceAdmin historyServiceAdmin;

    @Autowired
    private StationService stationService;

    @Autowired
    private OuFuService ouFuService;

    public BaseResult selectBlutdruckPage(Page page, String name, String community, String customerId,Integer partId) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BlutdruckCustom> mapList = blutdruckMapper.selectBlutdruckPage(name, community,customerId,partId);
        PageInfo pageInfo = new PageInfo<>(mapList);
        List<BlutdruckCustom> maps = mapList.stream().map(blutdruckCustom -> {
            Map map = JsonUtils.jsonToPojo(JsonUtils.objectToJson(blutdruckCustom), Map.class);
            String result = healthService.judgeIsNormal(map);
            blutdruckCustom.setResult(result);
            return blutdruckCustom;

        }).collect(Collectors.toList());

        pageInfo.setList(maps);
        return BaseResult.ok(pageInfo);
    }

    public BaseResult insert(Blutdruck blutdruck) {
        blutdruck.setId(IDUtils.genItemId());
        blutdruck.setCheckTime(DateUtils.formatTime(new Date()));
        blutdruck.setArrt1("手动添加");
        blutdruckMapper.insertSelective(blutdruck);
        healthService.judgeIsNormalAndInsertRecord(blutdruck);

        //插入操作记录
        historyServiceAdmin.generate("新增血压仪记录:" + blutdruck.getId(), OperateCategory.INSERT);
        return BaseResult.ok();
    }

    public BaseResult update(Blutdruck blutdruck) {
        blutdruckMapper.updateByPrimaryKeySelective(blutdruck);
        return BaseResult.ok();
    }

    public BaseResult delete(String id) {
        blutdruckMapper.deleteByPrimaryKey(id);
        return BaseResult.ok();
    }

    public BaseResult selectBlutdruckPageGroup(Page page, String name, String community) {
        Integer stationId = stationService.getLoginUserStation();
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BlutdruckCustom> mapList = blutdruckMapper.selectBlutdruckPageGroup(name, community,stationId);
        PageInfo pageInfo = new PageInfo<>(mapList);
        List<BlutdruckCustom> maps = mapList.stream().map(blutdruckCustom -> {
            Map map = JsonUtils.jsonToPojo(JsonUtils.objectToJson(blutdruckCustom), Map.class);
            String result = healthService.judgeIsNormal(map);
            blutdruckCustom.setResult(result);
            return blutdruckCustom;

        }).collect(Collectors.toList());

        pageInfo.setList(maps);
        return BaseResult.ok(pageInfo);
    }

    public BaseResult updatePart(Map map) {
        List ids = (List) map.get("ids");
        Integer partId = (Integer) map.get("partId");
        if (partId == null) {
            return BaseResult.build(StatusCode.NO_PART_ID);
        }
        int i = blutdruckMapper.updatePart(ids, partId);
        return BaseResult.ok(i);
    }

    /**
    * @Description: 通过用户id查询血压仪或者oufu手表血压
    * @Param [customerId, scope]
    * @Return com.dwxt.common.base.BaseResult
    * @Author chendq
    * @Date 2021/12/2
    **/
    public BaseResult selectBlutdruckListForTV(String customerId, Integer scope) {
        List<BlutdruckCustom> blutdruckCustoms = blutdruckMapper.selectBloodPressureList(customerId,scope,0);
        if (blutdruckCustoms.size() > 0) {
            return BaseResult.ok(blutdruckCustoms);
        }
        List<OuFuData> ouFuDataList = ouFuService.selectCustomerOufu(customerId, scope, "8");
        if (ouFuDataList.size() > 0) {
            BlutdruckCustom blutdruckCustom = new BlutdruckCustom();
            List<BlutdruckCustom> collect = ouFuDataList.stream().map(ouFuData -> {
                blutdruckCustom.setCustomerId(Integer.valueOf(customerId));
                blutdruckCustom.setHighData(ouFuData.getDiastolic().toString());
                blutdruckCustom.setLowData(ouFuData.getShrink().toString());
                blutdruckCustom.setCheckTime(DateUtil.parseStr(ouFuData.getCheckTime(), DateUtil.FMT_YYYYMMDD_HHMMSS));
                return blutdruckCustom;
            }).collect(Collectors.toList());
            return BaseResult.ok(collect);
        }
        return BaseResult.ok();
    }
}
